import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-06-04
 * Time: 8:16
 */
class Solution2 {
    List<String> ret;
    public List<String> binaryTreePaths(TreeNode root) {
        ret = new ArrayList<>();
        dfs(root,new StringBuffer());
        return ret;
    }

    void dfs(TreeNode root,StringBuffer _path) {
        StringBuffer path = new StringBuffer(_path);
        path.append(Integer.toString(root.val));
        if(root.left == null && root.right == null) {
            ret.add(path.toString());
            return;
        }
        path.append("->");
        if(root.left != null) {
            dfs(root.left,path);
        }
        //恢复现场
        if(root.right != null) {
            dfs(root.right,path);
        }
        //剪枝
        if(root == null) {
            return;
        }
    }
}
